// pages/order/detail/detail.js
let util = require('../../../utils/index')
import http from '../../../services/http/index'
import { PlainData } from '../../../utils/Data'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordersType: 0,
    ordersId: '',//当前订单id
    data: {},//详情
    map: false,
    markers: [{
      latitude: 23.21229,
      longitude: 113.324520,
      width: 50,
      height: 40,
      callout: {
        content: '出发地',
        color: '#fff',
        bgColor: '#F1604F',
        display: 'ALWAYS',
        padding: 20,
        borderRadius: 40
      },
    },
    {
      latitude: 23.18229,
      longitude: 113.3245211,
      width: 50,
      height: 40,
      callout: {
        content: '目的地',
        color: '#fff',
        bgColor: '#F1604F',
        display: 'ALWAYS',
        padding: 20,
        borderRadius: 40
      },
    }],
    polyline: [{
      points: [],
      color: "#FF0000DD",
      width: 4,
      dottedLine: true,
    }],
    //弹窗
    alert:{
      show:false,
      type:1,
      title:'',
      text1:'',
      text2:''
    },
    //评价
    judgeHide:false
  },
  //查询航班
  airSearch(e){
    let flightNo = this.data.data.info.planecode
    let date = this.data.data.info.use_time;
    console.log('../../air/search/search?flightNo=' + flightNo + '&date=' + date)
    util.default.go('../../air/search/search?flightNo=' + flightNo+'&date='+date)
  },
  //跳转发消息
  sendMessage(e) {
    let id = e.currentTarget.dataset['id'];
    util.default.go('../../my/message/messageChart?id=' + id);
  },

  // 打电话：
  callPhone(e) {
    let phoneNumber = e.currentTarget.dataset['telphone'];
    if (!phoneNumber) {
      wx.showToast({
        icon: 'none',
        title: '该订单没有提供电话号码'
      })
      return false;
    }
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  //去导航
  goNewLocation(e){
    var latitude = e.currentTarget.dataset['latitude'];
    var longitude = e.currentTarget.dataset['longitude'];
    var gps = {
      latitude: latitude,
      longitude: longitude
    };
    console.log(gps)
    if (!latitude || !longitude){
      wx.showToast({
        icon:'none',
        title:'经纬度不存在'
      })
      return false;
    }
    wx.openLocation({
      latitude: Number(gps.latitude),
      longitude: Number(gps.longitude),
      scale: 28
    });
  },

  //复制文本
  copyText(e){
    var text = e.currentTarget.dataset['text']; 
    wx.setClipboardData({
      data: text,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
         icon:'success'
        })
      }
    });
  },
  //取消转单
  cancelTransfer() {
    var _this = this;
    _this.setData({
      alert: {
        show: true,
        type: 2,
        title: '取消转单',
        text1: '您确定取消转单吗？ '
      }
    });
  },
  //转单
  transferOrder() {
    var _this = this;
    wx.showToast({
      title: '请求中',
      icon: 'loading',
      duration: 10000
    });

    var succCal = function (data) {
      wx.hideToast()
      var limit = data.limit;
      _this.setData({
        alert: {
          show: true,
          type: 1,
          title: '确认转单',
          text1: '您确定要转订单吗？ ',
          text2: '每月转单超过' + limit +'次，会有惩罚哦'
        }
      })
    }

    if (this.data.ordersType == 0) {
      //接机单
      http.myMissionTransfer()
        .then(data => {
          succCal(data);
        }).catch(error => {
          wx.hideToast()
          wx.showToast({
            title: error.msg || '查询转单失败',
            duration: 2000
          })
        });
    }
    if (this.data.ordersType == 3) {
      //预约用车
      http.myAppointTransfer()
        .then(data => {
          succCal(data);
        }).catch(error => {
          wx.hideToast()
          wx.showToast({
            title: error.msg || '查询转单失败',
            duration: 2000
          })
        });
    }

    if (this.data.ordersType == 2) {
      //班车
      http.myBusTransfer()
        .then(data => {
          succCal(data);
        }).catch(error => {
          wx.hideToast()
          wx.showToast({
            title: error.msg || '查询转单失败',
            duration: 2000
          })
        });
    }


  },
  //获取详情
  getDetail() {
    let param = { id: this.data.ordersId };
    var markers = this.data.markers;
    var polyline = this.data.polyline;
    var map = false;
    wx.showToast({
      title: '请求中',
      icon: 'loading',
      duration: 10000,
      mask:true
    });
    var succCal = function (data, _this) {
      wx.hideToast();
      if (_this.data.ordersType == 0 || _this.data.ordersType == 3){
        //接机单 预约用车
        if (data.info.routes.wayPoints.length > 0) {
          markers[0].latitude = Number(data.info.routes.wayPoints[0].latitude);
          markers[1].latitude = Number(data.info.routes.wayPoints[1].latitude);
          markers[0].longitude = Number(data.info.routes.wayPoints[0].longitude);
          markers[1].longitude = Number(data.info.routes.wayPoints[1].longitude);
          data.info.routes.waps.forEach(function (el, i) {
            el.latitude = Number(el.latitude);
            el.longitude = Number(el.longitude)
          })
          polyline[0].points = data.info.routes.waps
          map = true;
          wx.createMapContext('map').includePoints({
            padding:[10],
            points:polyline[0].points
          })
        };
      }
      if (_this.data.ordersType == 2){
        //班车
        markers[0].latitude =Number(data.bording[0].lat);
        markers[0].longitude = Number(data.bording[0].lng);
        markers[1].latitude = Number(data.get_off[0].lat);
        markers[1].longitude = Number(data.get_off[0].lng);
        polyline[0].points = [
          {
            latitude: Number(data.bording[0].lat),
            longitude: Number(data.bording[0].lng)
          },
          {
            latitude: Number(data.get_off[0].lat),
            longitude: Number(data.get_off[0].lng)
          }
        ]
        map = true
        wx.createMapContext('map').includePoints({
          padding:[10],
          points:polyline[0].points
        })
      }

      _this.setData({
        'data': Object.assign(new PlainData(), data),
        'markers': markers,
        'polyline': polyline,
        'map': map
      });

      //是否展示评论弹窗
      if (data.show_comment == 1){
        _this.judge();
      }
    }
    if (this.data.ordersType == 0) {
      //接机单
      http.myMissionDetail(param)
        .then(data => {
          succCal(data, this);
        });
    }
    if (this.data.ordersType == 3) {
      //预约用车
      http.myAppointDetail(param)
        .then(data => {
          succCal(data, this);
        })
    }
    if (this.data.ordersType == 2) {
      //班车
      http.myBusDetail(param)
        .then(data => {
          succCal(data, this);
        });
    }
    if (this.data.ordersType == 1) {
      //包车
      http.charterDetail(param)
          .then(data => {
            succCal(data, this);
          })
    }
  },
  //查看全部乘客信息
  allPassengers(){
    let id = this.data.data.info.id;
    util.default.go(`../passengers/passengers?id=` + id);
  },
  //取消弹窗
  cancelAlert(){
    this.setData({
      alert: {
        show: false,
        type:1,
        title: '',
        text1: '',
        text2: ''
      }
    })
  },
  //评价
  judge() {
    //包车没有评价
    var ordersId = this.data.ordersId;
    var ordersType = this.data.ordersType;
    this.setData({
      judgeHide:true
    })
  },
  //确认弹窗
  alertOk(){
    let type = this.data.alert.type;
    var _this = this;
    this.cancelAlert();
    if(type == 1){
      //确认转单
      let param = { id: _this.data.ordersId }
      //查询可以转单后
      if (_this.data.ordersType == 0) {
        //接机单
        http.sureTransfer(param)
          .then(data => {
            _this.getDetail();
            wx.showToast({
              title: '转单成功',
              duration: 2000
            })
          }).catch(error => {
            wx.showToast({
              title: error.msg || '转单失败',
              duration: 2000
            })
          })
      }
      if (_this.data.ordersType == 3) {
        //预约用车
        http.sureAppointTransfer(param)
          .then(data => {
            _this.getDetail();
            wx.showToast({
              title: '转单成功',
              duration: 2000
            })
          }).catch(error => {
            wx.showToast({
              title: error.msg || '转单失败',
              duration: 2000
            })
          })
      }

      if (_this.data.ordersType == 2) {
        //班车
        http.sureBusTransfer(param)
          .then(data => {
            _this.getDetail();
            wx.showToast({
              title: '转单成功',
              duration: 2000
            })
          }).catch(error => {
            wx.showToast({
              title: error.msg || '转单失败',
              duration: 2000
            })
          })
      }
    };

    if(type == 2){
      //取消转单
      let param = { id: _this.data.ordersId };
      wx.showToast({
        title: '请求中',
        icon: 'loading',
        mask: true,
        duration: 10000
      });

      var succCal = function (data) {
        wx.hideToast();
        _this.getDetail();
        wx.showToast({
          title: '取消转单成功',
          duration: 2000
        })
      };
      var errCal = function (error) {
        wx.hideToast();
        wx.showToast({
          title: error.msg || '取消转单失败',
          duration: 2000
        })
      }

      if (_this.data.ordersType == 0) {
        //接机单
        http.cancleTransfer(param)
          .then(data => {
            succCal(data)
          }).catch(error => {
            errCal()
          })
      };
      if (_this.data.ordersType == 3) {
        //预约用车
        http.cancleAppointTransfer(param)
          .then(data => {
            succCal(data)
          }).catch(error => {
            errCal()
          })
      }

      if (_this.data.ordersType == 2) {
        //班车
        http.cancleBusTransfer(param)
          .then(data => {
            succCal(data)
          }).catch(error => {
            errCal()
          })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type;
    let id = options.id;
    console.log(options)
    this.setData({
      ordersType: type,
      ordersId: id,
    });
    this.getDetail();
    var title = '订单详情'
    if (type == 2){
      title='班车详情'
    };
    wx.setNavigationBarTitle({
      title: title
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: ''
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})